﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.BUS;

namespace QuanLyKhachSan
{
    public partial class frmQuanLyTaiKhoan : Form
    {
        // Khai bao bien toan cuc cho danh sach nguoi dung
        List<NguoiDungDTO> dsNguoiDung = new List<NguoiDungDTO>();

        public frmQuanLyTaiKhoan()
        {
            InitializeComponent();
        }
        // Kiem tra du lieu nhap vao
        private bool KiemTraNhapLieu()
        {
            // Ten dang nhap ko dc de trong
            if (string.IsNullOrEmpty(txtTenDangNhap.Text ) == true)//neu no ko trong moi kiem tra tiep
            {
                MessageBox.Show("Ten Dang Nhap khong duoc de trong ", "Thong bao ");
                txtTenDangNhap.Focus();
                return false;
            }
            // Mat khau khong dc de trong
            if (string.IsNullOrEmpty(txtMatKhau.Text ) == true)//neu no ko trong moi kiem tra tiep
            {
                MessageBox.Show("Mat Khau khong duoc de trong ", "Thong bao ");
                txtMatKhau.Focus();
                return false;
            }
            // Ten nguoi dung ko dc de trong
            if (string.IsNullOrEmpty(txtTenND.Text) == true)//neu no ko trong moi kiem tra tiep
            {
                MessageBox.Show("Ten Nguoi Dung khong duoc de trong ", "Thong bao ");
                txtTenND.Focus();
                return false;
            }
            // Ten dem co the de trong, co du lieu thi kiem tra.
            
            if (DTO.NguoiDungDTO.KiemTraHoTen(txtTenDemND.Text) == false)
            {
                MessageBox.Show("Ten Dem khong hop le ", "Thong bao ");
                txtTenDemND.Focus();
                return false;
            }
            
            // kiem tra ten dang nhap
            if (DTO.NguoiDungDTO.KiemTraHoTen(txtTenND.Text) == false)
            {
                MessageBox.Show("Ten Nguoi Dung khong hop le ", "Thong bao ");
                txtTenND.Focus();
                return false;
            }
            // Ho cua tai khoan nguoi dung ko duoc de trong
            if (string.IsNullOrEmpty(txtHoND.Text) == true)
            {
                MessageBox.Show("Ho Nguoi Dung khong duoc de trong ", "Thong bao ");
                txtHoND.Focus();
                return false;
            }
            // kiem tra ho cua tai khoan nguoi dung
            if (DTO.NguoiDungDTO.KiemTraHoTen(txtHoND.Text) == false)
            {
                MessageBox.Show("Ho Nguoi Dung khong hop le ", "Thong bao ");
                txtHoND.Focus();
                return false;
            }
            // CMND ko dc de trong

            if (string.IsNullOrEmpty(txtCMND.Text ) == true)//neu no ko trong moi kiem tra tiep
            {
                MessageBox.Show("CMND khong duoc de trong ", "Thong bao ");
                txtCMND.Focus();
                return false;
            }
            // Kiem tra chung minh nhan dan co hop le hay khong
            if (DTO.NguoiDungDTO.KiemTraCMND(txtCMND.Text) == false)
            {
                MessageBox.Show("CMND phai la so co 9 chu so ", "Thong bao ");
                txtCMND.Focus();
                return false;
            }

            // Dia chi co the de trong, co gia tri thi kiem tra gia tri nhap vao
            
            
            if (DTO.NguoiDungDTO.KiemTraDiaChi(txtDiaChi.Text) == false)
            {
                MessageBox.Show("Dia chi khong hop le ", "Thong bao ");
                txtDiaChi.Focus();
                return false;
            }
            return true;
        }
        public bool KiemTraNguoiDung(NguoiDungDTO nguoiDungdto)
        {
            foreach (NguoiDungDTO nguoiDung in dsNguoiDung)
            {
                if (nguoiDung.TenDangNhap == nguoiDungdto.TenDangNhap)
                {
                    MessageBox.Show("Ten dang nhap da ton tai trong he thong ", "Thong bao ");
                    txtTenDangNhap.Focus();
                    return false;
                }
                
                if (nguoiDung.CMND1 == nguoiDungdto.CMND1)
                {
                    MessageBox.Show("CMND da ton tai trong he thong", "Thong bao ");
                    txtCMND.Focus();
                    return false;
                }
                
            }
            return true;
        }
        public NguoiDungDTO LayThongTinTuTextBox()
        {
            NguoiDungDTO nguoiDungdto = new NguoiDungDTO();
            nguoiDungdto.TenDangNhap = txtTenDangNhap.Text;
            nguoiDungdto.MatKhau = NguoiDungDTO.MaHoaMD5(txtMatKhau.Text);
            nguoiDungdto.HoNguoiDung = txtHoND.Text;
            nguoiDungdto.TenLot = txtTenDemND.Text;
            nguoiDungdto.TenNguoiDung = txtTenND.Text;
            nguoiDungdto.LoaiNguoiDung = cboLoaiNguoiDung.SelectedIndex;
            nguoiDungdto.CMND1 = txtCMND.Text;
            nguoiDungdto.DiaChi = txtDiaChi.Text;

            return nguoiDungdto;
        }

        private void frmQuanLyTaiKhoan_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            // An cac textbox
            txtTenDangNhap.Enabled = false;
            txtMatKhau.Enabled = false;

            txtTenND.Enabled = false;
            txtHoND.Enabled = false;
            txtTenDemND.Enabled = false;
            txtDiaChi.Enabled = false;
            txtCMND.Enabled = false;
            cboLoaiNguoiDung.Enabled = false;

            btnLuu.Enabled = false;
            btnHuy.Visible = false;
            btnThoat.Visible = true ;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;

            // Hien thi thong tin len combobox
            cboLoaiNguoiDung.Items.Add("Quản Lý");
            cboLoaiNguoiDung.Items.Add("Lễ Tân");
            cboLoaiNguoiDung.SelectedIndex = 0;

            // Danh sach cac tai khoan co trong he thong
            DataTable dt = new DataTable();
            dt = NguoiDungBUS.LayDSNguoiDung();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    NguoiDungDTO  nguoiDungdto = new NguoiDungDTO();
                    nguoiDungdto.TenDangNhap= dr["tenDangNhap"].ToString();
                    nguoiDungdto.CMND1 = dr["CMND"].ToString();

                    dsNguoiDung.Add(nguoiDungdto);  
                }
            }


        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            txtTenDangNhap.Enabled = true;
            txtMatKhau.Enabled = true;

            txtTenND.Enabled = true;
            txtHoND.Enabled = true;
            txtTenDemND.Enabled = true;
            txtDiaChi.Enabled = true;
            txtCMND.Enabled = true;
            cboLoaiNguoiDung.Enabled = true;

            btnLuu.Enabled = true;
            btnHuy.Visible = true;
            btnThem.Enabled = false;
            btnXoa.Enabled = false;
            btnThoat.Visible = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachNguoiDung.SelectedRows.Count > 0)
            {
                bool kq = false;
                NguoiDungDTO nguoiDungdto = new NguoiDungDTO();
                nguoiDungdto.TenDangNhap = txtTenDangNhap.Text;
                DialogResult result = MessageBox.Show("Ban chac chan muon xoa khong?", "Thong Bao", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    kq = NguoiDungBUS.Delete(nguoiDungdto); ;
                }
                if (kq == true)
                {
                    MessageBox.Show("Xoa Tai khoan nguoi dung thanh cong ", "Thong bao ");
                    // Hien thi thong tin vua nhap
                    DataTable dt = new DataTable();
                    dt = NguoiDungBUS.LayDSNguoiDung();
                    dgvDanhSachNguoiDung.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("Xoa tai khoan nguoi dung khong thanh cong ", "Thong bao ");
                    dgvDanhSachNguoiDung.Focus();
                }
            }
            else
                MessageBox.Show("Vui Long Chon Tai Khoan Muon Xoa");
            btnXoa.Enabled = true;
            
        }
        public void HienThiThongTin(NguoiDungDTO nguoiDungdto)
        {
            // Hien thi cac button
            btnLuu.Enabled = false;
            btnHuy.Visible = false;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Visible = true;
            // Hien thi thong tin vua nhap
            DataTable dt = new DataTable();
            dt = NguoiDungBUS.LayDSNguoiDungTheoTenNguoiDung(nguoiDungdto.TenNguoiDung);
            dgvDanhSachNguoiDung.DataSource = dt;

            string maLoaiND = dgvDanhSachNguoiDung.SelectedRows[0].Cells[5].Value.ToString();
            if (maLoaiND == "0")
                cboLoaiNguoiDung.SelectedIndex = 0;
            else
                cboLoaiNguoiDung.SelectedIndex = 1;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            
            bool kq = false;
            NguoiDungDTO nguoiDungdto = new NguoiDungDTO();
            if (KiemTraNhapLieu() == true)
            {
                nguoiDungdto = LayThongTinTuTextBox();
                if (KiemTraNguoiDung(nguoiDungdto) == false )
                {
                    kq = false;
                    txtTenDangNhap.Focus();
                }
                else
                { 
                    kq = NguoiDungBUS.Insert(nguoiDungdto);
                }
                if (kq == true)
                {

                    MessageBox.Show("Luu thong tin Tai Khoan thanh cong ", "Thong bao ");
                    HienThiThongTin(nguoiDungdto);
                }
                else
                {
                    MessageBox.Show("Luu thong tin Tai Khoan khong thanh cong ", "Thong bao ");
                    txtTenDangNhap.Focus();
                }
            }
           
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachNguoiDung_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachNguoiDung.SelectedRows.Count > 0)
            {
                txtTenDangNhap.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[0].Value.ToString();
                txtMatKhau.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[1].Value.ToString();
                txtHoND.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[2].Value.ToString();
                txtTenDemND.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[3].Value.ToString();
                txtTenND.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[4].Value.ToString();
                string maLoaiND = dgvDanhSachNguoiDung.SelectedRows[0].Cells[5].Value.ToString();
                txtCMND.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[6].Value.ToString();
                txtDiaChi.Text = dgvDanhSachNguoiDung.SelectedRows[0].Cells[7].Value.ToString();
                if (maLoaiND == "0")
                    cboLoaiNguoiDung.SelectedIndex = 0;
                else
                    cboLoaiNguoiDung.SelectedIndex = 1;
            }
            else
            {
                txtTenDangNhap.Text = "";
                txtMatKhau.Text = "";
                txtHoND.Text = "";
                txtTenDemND.Text = "";
                txtTenND.Text = "";
                
                txtCMND.Text = "";
                txtDiaChi.Text = "";
            }
        }

        private void txtTenTimKiem_TextChanged(object sender, EventArgs e)
        {
            string tenTK = txtTenTimKiem.Text;
            DataTable dt = new DataTable();
            dt = NguoiDungBUS.LayDSNguoiDungTheoTenNguoiDung(tenTK);
            dgvDanhSachNguoiDung.DataSource = dt;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // Hien thi cac thong tin
            btnThoat.Visible = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;

            // An cac thong tin
            btnHuy.Visible = false;
            btnLuu.Enabled = false;

            // An cac textbox
            txtTenDangNhap.Enabled = false;
            txtMatKhau.Enabled = false;

            txtTenND.Enabled = false;
            txtHoND.Enabled = false;
            txtTenDemND.Enabled = false;
            txtDiaChi.Enabled = false;
            txtCMND.Enabled = false;
            cboLoaiNguoiDung.Enabled = false;
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

       

        
    }
}